package com.handu.luna;

//import java.util.Arrays;
//import java.util.List;
//import java.util.function.Consumer;


import java.util.List;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.transaction.Transactional;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.handu.luna.boss.controller.support.PermissionInterceptor;
import com.handu.luna.core.domain.support.EnumAwareConvertUtilsBean;

/**
 * BOSS子项目启动类
 * @author zhangwen
 *
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Transactional
@EnableJpaAuditing
@ImportResource("classpath:applicationContext.xml")
public class Application extends WebMvcConfigurerAdapter {

	/**
	 * 增加springboot对分页的支持（使用pageable）
	 */
	@Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
    }
	
	/**
	 * 增加内置容器对utf-8系统的过滤器
	 * 如果没有这个过滤器，tomcat下回出现乱码，jetty没有太大问题
	 *
	 */
	@Configuration
	protected static class ApplicationContextFilterConfiguration {
		@Bean
		public Filter applicationContextIdFilter(ApplicationContext context) {
			CharacterEncodingFilter filter = new CharacterEncodingFilter();
			filter.setEncoding("UTF-8");
			return filter;
		}
	}		
	
	/**
	 * 开发时使用内嵌数据库名称
	 */
	@Value("${spring.datasource.name:testdb}")
	private String name = "testdb";
	
	/**
	 * spring mvc拦截器定义
	 * @return 用于权限管理的拦截器
	 */
	@Bean
	public PermissionInterceptor permissionInterceptor() {
	    return new PermissionInterceptor();
	}

	/**
	 * 为springboot增加spring mvc拦截器定义
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(permissionInterceptor());
	}
	
	/**
	 * springmvc上传文件配置类
	 * @return springmvc上传文件配置类
	 */
	@Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("50000000KB");
        factory.setMaxRequestSize("50000000KB");
        
        return factory.createMultipartConfig();
    }	
    public static void main(String[] args) {
    	BeanUtilsBean.setInstance(new BeanUtilsBean(new EnumAwareConvertUtilsBean()));
    	
    	SpringApplication app = new SpringApplication(Application.class);
        app.setShowBanner(true); 
        app.run(args);
    }
}